package service4gateway

import (
	Article "MyBlogv2/Servers/Article/article"
	"MyBlogv2/blog-common/HttpCode"
	"MyBlogv2/blog-common/helper"
	"MyBlogv2/blog-common/models"
	"MyBlogv2/blog-common/response"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"strconv"
	"time"
)

func GetCategoryList(c *gin.Context) {
	rsp, err := clientOfArticle.GetCategoryList(c, &Article.ArticleRequest{})
	if err != nil {
		response.ResponseError(c, HttpCode.FAILURE)
		return
	}
	var categoryVoList []models.CategoryVo
	copier.Copy(&categoryVoList, rsp.GetCategoryVoList())
	for i, v := range rsp.GetCategoryVoList() {
		categoryVoList[i].CreateTime = helper.TimestampToMyTime(v.CreateTime)
		categoryVoList[i].UpdateTime = helper.TimestampToMyTime(v.UpdateTime)
	}
	response.ResponseOK(c, categoryVoList)

}

func AddCategory(c *gin.Context) {
	var categoryDto models.CategoryDto
	if err := c.ShouldBindJSON(&categoryDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	categoryDto.Id = 0
	models.Db.Model(new(models.Category)).Create(&categoryDto)
	response.ResponseOK(c, nil)
	return
}

func GetCategoryList4Admin(c *gin.Context) {
	rsp, _ := clientOfArticle.GetCategoryList(c, &Article.ArticleRequest{})
	var categoryVoList []models.CategoryVo
	copier.Copy(&categoryVoList, rsp.CategoryVoList)
	for i, v := range rsp.CategoryVoList {
		categoryVoList[i].CreateTime = helper.TimestampToMyTime(v.CreateTime)
		categoryVoList[i].UpdateTime = helper.TimestampToMyTime(v.UpdateTime)
	}
	response.ResponseOK(c, categoryVoList)
	return
}

func SearchCategory(c *gin.Context) {
	var searchCategoryDto models.SearchCategoryDto
	if err := c.ShouldBindJSON(&searchCategoryDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	var categoryVoList []models.CategoryVo
	db := models.Db.Model(new(models.CategoryVo))
	if searchCategoryDto.CategoryName != "" {
		db.Where("category_name like ?", "%"+searchCategoryDto.CategoryName+"%")
	}
	if !time.Time(searchCategoryDto.StartTime).IsZero() {
		db.Where("created_at >= ?", searchCategoryDto.StartTime)
	}
	if !time.Time(searchCategoryDto.EndTime).IsZero() {
		db.Where("created_at <= ?", searchCategoryDto.EndTime)
	}
	db.Find(&categoryVoList)
	for i, v := range categoryVoList {
		var articleCount int64
		models.Db.Model(new(models.Article)).Where("category_id = ?", v.Id).Count(&articleCount)
		categoryVoList[i].ArticleCount = int(articleCount)
	}
	response.ResponseOK(c, categoryVoList)
	return

}

func GetCategoryById(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	var category models.CategoryVo
	models.Db.Model(new(models.CategoryVo)).Where("id = ?", id).Find(&category)
	var articleCount int64
	models.Db.Model(new(models.Article)).Where("category_id = ?", id).Count(&articleCount)
	category.ArticleCount = int(articleCount)
	response.ResponseOK(c, category)
	return

}

func AddCategory4Category(c *gin.Context) {
	var categoryDto models.CategoryDto
	if err := c.ShouldBindJSON(&categoryDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	categoryDto.Id = 0
	models.Db.Model(new(models.Category)).Save(&categoryDto)
	response.ResponseOK(c, nil)
	return
}

func AddOrUpdateCategory(c *gin.Context) {
	var categoryDto models.CategoryDto
	if err := c.ShouldBindJSON(&categoryDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	if categoryDto.Id == 0 {
		models.Db.Model(new(models.Category)).Create(&categoryDto)
	} else {
		models.Db.Model(new(models.Category)).Where("id = ?", categoryDto.Id).Updates(&categoryDto)
	}
	response.ResponseOK(c, nil)
	return

}

func DeleteCategory(c *gin.Context) {
	var ids []int
	if err := c.ShouldBindJSON(&ids); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	var articleCount int64
	models.Db.Model(new(models.Article)).Where("category_id in (?)", ids).Count(&articleCount)
	if articleCount > 0 {
		response.CommonResponse(c, HttpCode.FAILURE, nil, "存在文章引用该分类，无法删除")
		return
	}
	models.Db.Model(new(models.Category)).Where("id in (?)", ids).Delete(new(models.Category))
	response.ResponseOK(c, nil)
	return

}
